package com.robert.mvp.base;

import java.lang.ref.WeakReference;

/**
 * Des:
 *
 * @Author: cpf
 * @date: ${date} ${hour} ${minute}
 * @email: 2227039052@qq.com
 */
public abstract class BasePresenter<M,V> {
    protected M mModel;
    protected WeakReference<V> mViewRef;

    protected void onAttach(M model,V view){
            mModel = model;
            mViewRef = new WeakReference<>(view);
    }

    protected void onDetach(){
        if (null != mViewRef){
            mViewRef.clear();
            mViewRef=null;
        }
    }

    protected V getView(){
        return isViewAttached() ? mViewRef.get():null;
    }

    protected boolean isViewAttached() {
        return null != mViewRef &&  null != mViewRef.get();
    }
}
